package com.robotserver.orders.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.service.MiniProSubMsgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: jianghan
 * @Date: 2020/2/7 10:02
 * @Description:
 */
@Service
public class MiniProSubMsgServiceImpl implements MiniProSubMsgService {

    private static Logger logger = LoggerFactory.getLogger(MiniProSubMsgServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;
    @Value("${auth.invalid.sendurl}")
    private String authInvalidSendUrl;

    @Async("taskExecutor")
    @Override
    public void sendAuthInvalid(String type, String account, Long uid) {
        try {
            MultiValueMap<String,String> param = new LinkedMultiValueMap<>();
            param.add("type",type);
            param.add("account",account);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(param, headers);
            ResponseEntity<JSONObject> response = restTemplate.postForEntity(authInvalidSendUrl, request, JSONObject.class);
            if (response.getStatusCode() != HttpStatus.OK){
                logger.error("send fail net err.");
            }
            JSONObject jsonObject = response.getBody();
            if (Integer.parseInt(jsonObject.get("code").toString())!=200){
               logger.error("send err:{}",jsonObject.get("msg").toString());
            }else {
                logger.info("send request succ.");
            }
        } catch (Exception e) {
            logger.error("send err:{}",e);
        }
    }
}
